class Merchant < ActiveRecord::Base
  attr_accessor :images

  # scope
  default_scope -> { order("created_at DESC") }

  # default per_page value for kaminari
  paginates_per 6

  class << self
    def create_with_images(params)
      transaction do
        merchant_params = permit_params params
        merchant = create merchant_params

        if params[:images].present?
          params[:images].each do |base_64_encoded_data|
            Image.create(base_64_encoded_data: base_64_encoded_data,
                        image_type: IMAGE_TYPE["商户图片"],
                        owner: merchant.id)
          end
        end
      end
    end

    def find_with_images(merchant_id)
      merchant = select(:id,
                        :name,
                        :phone,
                        :business_scope,
                        :merchant_type,
                        :longitude,
                        :latitude,
                        :location,
                        :is_dtd,
                        :service_start,
                        :service_end).find_by(id: merchant_id)

      if merchant.present?
        result = {
          id: merchant.id,
          name: merchant.name,
          phone: merchant.phone,
          business_scope: merchant.business_scope,
          merchant_type: merchant.merchant_type,
          longitude: merchant.longitude,
          latitude: merchant.latitude,
          location: merchant.location,
          is_dtd: merchant.is_dtd,
          service_start: merchant.service_start,
          service_end: merchant.service_end
        }

        # result[:images] = Image.where(owner: merchant_id).pluck(:url)
        result[:images] = Image.where(owner: merchant_id).select(:id, :url)
      end
      
      result
    end

    def update_with_images(params)
      transaction do
        merchant_params = permit_params params
        update(params[:id], merchant_params)

        if params[:remove_image_ids].present?
          Image.destroy_all(owner: params[:id],
                          image_type: IMAGE_TYPE["商户图片"],
                          id: params[:remove_image_ids])

        end

        if params[:new_images].present?
          params[:new_images].each do |base_64_encoded_data|
            Image.create(base_64_encoded_data: base_64_encoded_data,
                        image_type: IMAGE_TYPE["商户图片"],
                        owner: params[:id])
          end
        end
      end
    end

    def permit_params(params)
      params_permit_names =  [:name,
                              :phone,
                              :business_scope,
                              :merchant_type,
                              :longitude,
                              :latitude,
                              :location,
                              :is_dtd,
                              :service_start,
                              :service_end,
                              :information_clerk_id]

      permit_params = params.select do |k, v|
        params_permit_names.include?(k.to_sym)
      end

      permit_params.to_hash
    end
  end
end